Q2 Program to generate sentence where subject is in ["ram","sham"] and verb is ["play","learn"] and object is["hockey",football"].from random import shuffle li=[3,6,7,8] shuffle(li) print(li) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== [6, 7, 8, 3]
Q3.Program to print list after removing all duplicate valuue from list.subject=["ram","sham"] verb=["play","learn"] object=["hockey","football"] for i in range(len(subject)): for j in range(len(verb)): for k in range(len(object)): sentence="%s %s %s" %(subject[i],verb[i],object[k]) print(sentence) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== ram play hockey ram play football ram play hockey ram play football sham learn hockey sham learn football sham learn hockey sham learn football
Q4.Program to find permutaion of string.def removeduplicate(li): newlist=[] for item in li: if item not in newlist: newlist.append(item) return(newlist) li=[1,1,2,3,4,3,4,5,5,4,3,2] print(removeduplicate(li)) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== [1, 2, 3, 4, 5]
Q5.Program to find permutaion of string and sort list in reverse order.result=[] def permu(data,i,n): if i==n: result.append("".join(data)) for j in range(i,n+1): data[i],data[j]=data[j],data[i] permu(data,i+1,n) data[i],data[j]=data[j],data[i] permu(list('abc'),0,2) print(result) Output >>> ==================== RESTART: C:\Users\Dell\Desktop\ss.py ==================== ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
result=[] def permu(data,i,n): if i==n: result.append("".join(data)) for j in range(i,n+1): data[i],data[j]=data[j],data[i] permu(data,i+1,n) data[i],data[j]=data[j],data[i] permu(list('abc'),0,2) d=sorted(result,reverse=True) print(d) Output >>> ==================== RESTART: C:\Users\Dell\Desktop\ss.py ==================== ['cba', 'cab', 'bca', 'bac', 'acb', 'abc']
0 Comments